import { useCallback, useState } from "react";
import useMount from "./useMount";

interface IOptions {
  params: Record<string, string>;
  manual?: boolean;
  onSuccess?: (res: unknown) => void;
  onError?: (err: unknown) => void;
}
/**
 * 自定义Hook: 发起请求
 * @param service 服务函数
 * @param params 参数
 * @returns [loading, data]
 */
const useRequest = (
  service: (params: Record<string, string>) => Promise<unknown>,
  options: IOptions
) => {
  const [data, setData] = useState<unknown>();
  const [loading, setLoading] = useState<boolean>(false);

  const init = useCallback(
    (curParams: Record<string, string>) => {
      setLoading(true);
      return service(curParams)
        .then((res) => {
          setData(res);
          setLoading(false);
          options?.onSuccess?.(res);
        })
        .catch((error) => {
          setLoading(false);
          options?.onError?.(error);
        });
    },
    [service]
  );

  useMount(() => {
    if (!options.manual) {
      init(options.params);
    }
  });

  const run = (runParams: Record<string, string>) => init(runParams);

  return { loading, data, run };
};

export default useRequest;
